<!-- 期末结账页面 -->
<template>
  <div class="margin-lr">
    <el-card>
      <el-form
        :inline="true"
        :model="searchCriteria"
        ref="searchCriteria"
        :rules="rules"
        size="mini"
        class="padding-top-sm"
      >
        <el-form-item prop="period" label="期间">
          <el-date-picker
            type="month"
            v-model="searchCriteria.period"
            style="width: 200px"
            value-format="yyyy-MM"
            placeholder="请选择期间"
          ></el-date-picker>
        </el-form-item>
        <el-button
          type="primary"
          size="mini"
          @click="submit()"
          v-loading.fullscreen.lock="fullscreenLoading"
          >期末结账</el-button
        >
      </el-form>
    </el-card>
    <el-card>
      <h4>
        期末结账成功，请到凭证列表页面查询
        <a
          :href="
            `#/main/finance__voucher__list?type=3&accountMonth=${accountMonth}`
          "
          target="_bank"
          >传送门</a
        >
      </h4>
    </el-card>
    <div class="padding color margin-top">
      已结帐凭证列表(反结帐、重新结帐、删除都得按照顺序来操作)
    </div>
    <el-card>
      <el-table
        class="table-box text-sm"
        size="mini"
        :data="tableData"
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        min-height="20em"
      >
        <el-table-column
          v-for="(item, index) in tableColumns"
          :key="index"
          :prop="item.prop"
          :label="item.label"
        >
          <template slot-scope="scope">
            <template v-if="item.prop === 'sysRemark'">
              <el-popover placement="right" width="400" trigger="hover">
                {{ scope.row["sysRemark"] }}
                <i slot="reference" class="el-icon-s-order" size="medium"></i>
              </el-popover>
            </template>
            <template v-else-if="item.prop === 'cuser'">
              {{ userMap[scope.row["cuser"]] }}
            </template>
            <template v-else>
              {{ scope.row[item.prop] }}
            </template>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-search"
              circle
              size="mini"
              @click="viewFinanceVoucher(scope.row.id)"
            ></el-button>
            <el-button
              type="danger"
              size="mini"
              @click="reverse(scope.row.id, scope.row.isLocked ? 0 : 1)"
              >{{ scope.row.isLocked ? "反结帐" : "重新结帐" }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { ajax } from "@/store/yg/common/utils";
import { m } from "@/store/yg/common/local";
export default {
  name: "finalTuningRemit",
  data() {
    return {
      searchCriteria: {
        period: ""
      },
      loading: true,
      fullscreenLoading: false,
      accountMonth: "",
      userMap: {},
      tableData: [],
      rules: {
        period: [
          {
            required: true,
            message: "请选择期间",
            trigger: "change"
          }
        ]
      },
      tableColumns: [
        { prop: "accountMonth", label: "期间" },
        { prop: "voucherNo", label: "凭证号" },
        { prop: "accountDate", label: "凭证日期" },
        { prop: "totalDebitMoney", label: "借方金额" },
        { prop: "totalCreditorMoney", label: "贷方金额" },
        { prop: "cuser", label: "制单人" },
        { prop: "cdate", label: "创建时间" },
        { prop: "sysRemark", label: "日志" }
      ]
    };
  },
  mounted() {
    this.list();
  },
  methods: {
    // 加载数据
    async list() {
      let res = await ajax("list", {});
      this.userMap = res.userMap;
      this.tableData = res.financeVouchers;
      this.loading = false;
    },
    // 期末结账按钮
    async submit() {
      const criteria = this.searchCriteria;
      this.$confirm("此操作是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.fullscreenLoading = true;
          ajax("submit", criteria).then(
            res => {
              this.fullscreenLoading = false;
              console.log(res);
              let success = res.success;
              if (!success) {
                this.$message.error(res.message);
                return;
              }
              this.accountMonth = this.getPreMonth(this.searchCriteria.period);
            },
            error => {
              this.fullscreenLoading = false;
              console.log(error);
            }
          );
        })
        .catch(() => {
          this.fullscreenLoading = false;
        });
    },
    // 反结转和重新结账
    async reverse(financeVoucherId, isLocked) {
      if (financeVoucherId === null || financeVoucherId === undefined) {
        this.$message.error("凭证ID为空");
        return;
      }
      if (isLocked === null || isLocked === undefined) {
        this.$message.error("是否锁定为空");
        return;
      }
      this.$confirm("是否继续此操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.fullscreenLoading = true;
          var data = {};
          data["financeVoucherId"] = financeVoucherId;
          data["isLocked"] = isLocked;
          ajax("reverse", data).then(
            res => {
              this.fullscreenLoading = false;
              var success = res.success;
              if (success) {
                // location.reload();
                this.$store.dispatch("loadPageData", this.$route.params.uri);
                this.$message.success("操作成功");
              } else {
                this.$message.error("操作失败");
              }
            },
            error => {
              this.fullscreenLoading = false;
              console.log(error);
            }
          );
        })
        .catch(() => {});
    },
    // 查看凭证
    viewFinanceVoucher(id) {
      if (id) {
        window.open("/#/blank/finance__voucher__view?pid=" + id);
      }
    },
    // 获取上一个月
    getPreMonth(date) {
      var arr = date.split("-");
      var year = arr[0];
      var month = arr[1];
      var year2 = year;
      var month2 = parseInt(month) - 1;
      if (month2 == 0) {
        year2 = parseInt(year2) - 1;
        month2 = 12;
      }
      if (month2 < 10) {
        month2 = "0" + month2;
      }
      var t2 = year2 + "-" + month2;
      return t2;
    }
  }
};
</script>
